//
//  AllocatedBalanceView.m
//  NtoccShipper
//
//  Created by 张天兴 on 2023/8/3.
//  Copyright © 2023 zhuchongjin. All rights reserved.
//

#import "AllocatedBalanceView.h"

 
@interface AllocatedBalanceView ()
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UILabel *yueLabel;
@property (nonatomic, strong) UILabel *allBanlanceLabel;
@property (nonatomic, strong) UILabel *distributableLabel;
@property (nonatomic, strong) UILabel *financialBalanceLabel;
@property (nonatomic, strong) UILabel *fenPBalanceLabel;
@end

@implementation AllocatedBalanceView


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame code:(NSString *)code{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.bgView];
        NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/shipment/virtualAmount/%@",CJBaseUrl,code);
        NSDictionary *dicPar = @{};
        WS(ws);
        [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
            ws.allBanlanceLabel.text = [NSString stringWithFormat:@"%@",responseObject[@"shipmentMerchantAmount"]];
            ws.financialBalanceLabel.text =  [NSString stringWithFormat:@"%@",responseObject[@"financeVirtualAmount"]];
            ws.distributableLabel.text = [NSString stringWithFormat:@"%@",responseObject[@"shipmentVirtualAmount"]];
        } failure:^(NSError * _Nonnull error) {

        }];
    }
    return self;
}
-(UIView *)bgView{
    if (_bgView == nil) {
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        [self.bgView addSubview:self.topView];
    }
    return _bgView;
}

- (UIView *)topView {
    if(_topView == nil){
        self.topView = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 300)/2, (SCREEN_HEIGHT - 250)/2 -50, 300, 250)];
        self.topView.backgroundColor = [UIColor whiteColor];
        self.topView.layer.masksToBounds = YES;
        self.topView.layer.cornerRadius = 10;
        for (int i = 0; i < 3; i++) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, i * 50, 120, 50)];
            label.font = [UIFont systemFontOfSize:15];
        
            UILabel *zhiLabel = [[UILabel alloc] initWithFrame:CGRectMake( 210, i * 50, 80, 50)];
            zhiLabel.font = [UIFont systemFontOfSize:13];
            zhiLabel.textAlignment = NSTextAlignmentRight;
            if(i == 0){
                self.allBanlanceLabel = zhiLabel;
                label.text = @"货主商户余额";
            }else if (i == 1){
                self.distributableLabel = zhiLabel;
                label.text = @"货主可分配余额";
            }else{
                self.financialBalanceLabel = zhiLabel;
                label.text = @"财务余额";
            }
            [_topView addSubview:label];
            [_topView addSubview:zhiLabel];
        }
        [_topView addSubview:self.fenPBalanceLabel];
        [_topView addSubview:self.textField];
        [_topView addSubview:self.comitterButton];
        [_topView addSubview:self.cententButton];
    }
    return _topView;
}
- (UILabel *)fenPBalanceLabel{
    if(_fenPBalanceLabel == nil){
        self.fenPBalanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,150, 100, 50)];
        self.fenPBalanceLabel.text = @"分配余额";
        self.fenPBalanceLabel.font = [UIFont systemFontOfSize:15];
    }
    return _fenPBalanceLabel;
}
-(UITextField *)textField{
    if(_textField  == nil){
        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(180, 155, 120, 40)];
        self.textField.keyboardType =  UIKeyboardTypeDecimalPad;
        self.textField.placeholder = @"请输入分配余额";
        self.textField.font = [UIFont systemFontOfSize:13];
    }
    return _textField;
}
- (UIButton *)cententButton{
    if(_cententButton == nil){
        self.cententButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        self.cententButton.frame = CGRectMake(10, 200, 100, 30);
        self.cententButton.layer.masksToBounds = YES;
        self.cententButton.layer.cornerRadius = 15;
        [self.cententButton setTitle:@"取消" forState:(UIControlStateNormal)];
        [self.cententButton setTitleColor:[UIColor  cjMainTextColor] forState:(UIControlStateNormal)];
        self.cententButton.layer.borderColor = [UIColor cjMainTextColor].CGColor;
        self.cententButton.layer.borderWidth = 1;
        self.cententButton.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    return _cententButton;
}
- (UIButton *)comitterButton{
    if(_comitterButton == nil){
        self.comitterButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        self.comitterButton.frame = CGRectMake(SCREEN_WIDTH - (SCREEN_WIDTH - 300)/2  - 180, 200, 100, 30);
        self.comitterButton.layer.masksToBounds = YES;
        self.comitterButton.layer.cornerRadius = 15;
        [self.comitterButton setTitle:@"分配" forState:(UIControlStateNormal)];
        [self.comitterButton setTitleColor:[UIColor cjMainColor] forState:(UIControlStateNormal)];
        self.comitterButton.layer.borderColor = [UIColor cjMainColor].CGColor;
        self.comitterButton.layer.borderWidth = 1;
        [self.comitterButton addTarget:self action:@selector(commentAction) forControlEvents:(UIControlEventTouchUpInside)];
        self.comitterButton.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    return _comitterButton;
}
- (void)commentAction{
    [self.textField resignFirstResponder];
    if(self.textField.text.length <= 0){
        [CJAlert showStringDismissWithDelay:@"请输入分配金额"];
        return;
    }
    if(([self.financialBalanceLabel.text floatValue] + [self.textField.text floatValue]) > [self.distributableLabel.text floatValue]){
        [CJAlert showStringDismissWithDelay:@"输入金额+财务余额不能大于可分配余额"];
        return;
    }
    if(self.blockText){
        [self.textField resignFirstResponder];
        self.blockText(@"");
    }
}
@end
